package org.twnmayberry.it;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJBTransactionRolledbackException;
import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.twndmv.dto.PersonDTO;
import org.twndmv.dto.PhysicalDetailsDTO;
import org.twnmayberry.bo.POI;
import org.twnmayberry.bo.Sex;
import org.twnmayberry.dto.POIDTO;
import org.twnmayberry.ejb.MayberryTestUtilRemote;
import org.twnmayberry.ejb.PoiMgmtRemote;



public class MayberryRollbackIT {
	private static Log log = LogFactory.getLog(MayberryRollbackIT.class);
	private InitialContext jndi;
	
    private static final String poiManagerJNDI = System.getProperty("jndi.name.poi",
            "twnMayberryWAR/PoiMgmtEJB!org.twnmayberry.ejb.PoiMgmtRemote");
    private static final String mayTestUtilJNDI = System.getProperty("jndi.name.mayberryTestUtil",
            "twnMayberryWAR/MayberryTestUtilEJB!org.twnmayberry.ejb.MayberryTestUtilRemote");
    

    private MayberryTestUtilRemote mayTestUtil;
    private PoiMgmtRemote poiManager;

	@Before
	public void setUp() throws Exception {
		log.debug("===== TestDmvRollbackIT.init =====");

	        jndi = new InitialContext();
	        jndi.lookup("/"); //do a quick comms check of JNDI

	        
	        poiManager = (PoiMgmtRemote)jndi.lookup(poiManagerJNDI);
	        mayTestUtil = (MayberryTestUtilRemote)jndi.lookup(mayTestUtilJNDI);
	       
	        assertNotNull(poiManager);
	        assertNotNull(mayTestUtil);
	}
	
	@Test
	public void testRollback() {
		log.info("===== testRollback =====");
		mayTestUtil.resetAll();
		//test to make sure no people currently
		assertEquals(poiManager.getAllPOI().size(), 0);
		List<POIDTO> people = new ArrayList<POIDTO>();
		
		for(int i = 0; i < 10; i++) {
			POIDTO testPOI = new POIDTO();
			testPOI.setFirstName("Steve");
			testPOI.setLastName("Jobs");
			testPOI.setGender(Sex.MALE);
	        testPOI.setHeight(64); //in inches
		    testPOI.setWeight(180);
	        people.add(testPOI);
		}
		assertEquals(people.size(), 10);
		
		//add a good list of people and see it works.
		poiManager.addList(people); 
		assertEquals(poiManager.getAllPOI().size(), 10);
		List<POIDTO> people2 = new ArrayList<POIDTO>();
		
		for(int i = 0; i < 9; i++) {
			POIDTO testPOI = new POIDTO();
			testPOI.setFirstName("Steve");
			testPOI.setLastName("Jobs");
			testPOI.setGender(Sex.MALE);
	        testPOI.setHeight(64); //in inches
		    testPOI.setWeight(180);
	        people2.add(testPOI);
        }
		POIDTO testPOI = new POIDTO();
		testPOI.setFirstName(null);
		testPOI.setLastName("Jobs");
		testPOI.setGender(Sex.MALE);
        testPOI.setHeight(64); //in inches
	    testPOI.setWeight(180);
        people2.add(testPOI); 
	    
		assertEquals(people2.size(), 10);
		try {
		  poiManager.addList(people2);
		} catch(EJBTransactionRolledbackException re) {
			log.info("Additions rolled back!");
			assertNotNull(re);
		}
		
		//Show the transaction failed cuz the tenth person has no first name...
		//if it passed, there would be 20 but there's still 10
		assertEquals(poiManager.getAllPOI().size(), 10);
	}
}
